﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using Ensurance.Properties;

namespace Ensurance.Constraints.Prefix
{
    public class PropertyConstraint<T> : PrefixConstraint<T>
    {
        private string _name;
        private bool _propertyExists;
        private T _propValue;

        /// <summary>
        /// Initializes a new instance of the <see cref="PropertyConstraint"/>
        /// class.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="baseConstraint">The constraint to apply to the property.</param>
        public PropertyConstraint(string name, Constraint<T> baseConstraint)
            : base(baseConstraint)
        {
            _name = name;
        }

        /// <summary>
        /// Test whether the constraint is satisfied by a given value
        /// </summary>
        /// <param name="actual">The value to be tested</param>
        /// <returns>True for success, false for failure</returns>
        public override bool IsSatisfiedBy(T actual)
        {
            Actual = actual;

            // TODO: Should be argument exception?
            if (ReferenceEquals(actual, null))
            {
                return false;
            }

            PropertyInfo property = actual.GetType().GetProperty(_name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            if (property == null)
            {
                return _propertyExists = false;
            }

            if (BaseConstraint == null)
            {
                return true;
            }

            _propValue = (T)property.GetValue(actual, null);
            return BaseConstraint.IsSatisfiedBy(_propValue);
        }

        /// <summary>
        /// Write the constraint description to a MessageWriter
        /// </summary>
        /// <exception cref="ArgumentNullException">if the message writer is null.</exception>
        /// <param name="writer">The writer on which the description is displayed</param>
        public override void WriteDescriptionTo(IMessageWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            writer.WritePredicate(string.Format(Resources.PropertyName_1, _name));
            BaseConstraint.WriteDescriptionTo(writer);
        }

        /// <summary>
        /// Write the actual value for a failing constraint test to a
        /// MessageWriter. The default implementation simply writes the raw
        /// value of actual, leaving it to the writer to perform any formatting.
        /// </summary>
        /// <exception cref="ArgumentNullException">if the message writer is null.</exception>
        /// <param name="writer">The writer on which the actual value is displayed</param>
        public override void WriteActualValueTo(IMessageWriter writer)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            if (_propertyExists)
            {
                writer.WriteActualValue(_propValue);
            }
            else
            {
                writer.WriteActualValue(Actual.GetType());
            }
        }
    }
}
